let ctx = wx.createCanvasContext('myCanvas')
Page({
    data: {
        mosaic: true,
        imagePath: '',
        isAuthSavePhoto: false,
    },
    open() {
        wx.chooseImage({
            count: 1,
        }).then(res => {
            console.log(res)
            let file = res.tempFilePaths[0]
            ctx.drawImage(file, 0, 0, 320, 320)
            ctx.draw()
        })
    },
    clear() {
        this.setData({
            mosaic: false
        })
    },
    mosaic() {
        this.setData({
            mosaic: true
        })
    },
    move(e) {
        console.log(e)
        if (this.data.mosaic) {
            ctx.setFillStyle('aquamarine')
            ctx.fillRect(e.touches[0].x, e.touches[0].y, 10, 10)
            ctx.fillRect(e.touches[0].x + 10, e.touches[0].y + 10, 10, 10)
            ctx.setFillStyle('black')
            ctx.fillRect(e.touches[0].x + 10, e.touches[0].y, 10, 10)
            ctx.fillRect(e.touches[0].x, e.touches[0].y + 10, 10, 10)
        } else {
            ctx.clearRect(e.touches[0].x, e.touches[0].y, 20, 20)
        }
        ctx.draw(true)
    },
    save() {
        //获取临时路径
        wx.canvasToTempFilePath({
            canvasId: 'myCanvas',
        }).then(res => {
            console.log('savefile:', res)
            this.setData({
                imagePath: res.tempFilePath
            })
            wx.saveImageToPhotosAlbum({
                filePath: this.data.imagePath,
                success: (res) => {
                    wx.showModal({
                        title: '保存成功',
                        icon: "success",
                        duration: 1000
                    })
                    console.log(res)
                },
                fail: (err) => {
                    console.log(err)
                }
            })
        })

    },
    seal() {
        ctx.setFillStyle('red')
        ctx.setFontSize(36)
        ctx.fillText('07200826凌子琦', 0, 200)
        ctx.draw(true)
    },

})